package top.hcode.hoj.collstat;

import lombok.Data;
import org.nutz.lang.Times;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class SchoolStatReportGenerator {
    // 考试统计数据模型
    public static class ExamStats {
        String schoolName;
        String campusName;

        int totalStudents;
        int submittedStudents;
        int totalClasses;
        List<ClassInfo> topClasses;

        // 客观题统计
        int minScore;
        int maxScore;
        double avgScore;
        int highScore;
        double highScoreRate;
        Map<String, Double> scoreDistribution;
        List<StudentRank> topStudents;

        // 时间统计
        int submissionCount;
        int minTime;
        int maxTime;
        double avgTime;
        Map<String, Double> timeDistribution;
    }

    public static class ClassInfo {
        String className;
        int studentCount;

        public ClassInfo(String className, int studentCount) {
            this.className = className;
            this.studentCount = studentCount;
        }
    }

    @Data
    public static class StudentRank {
        int rank;
        String className;
        String name;
        int score;
        Date submitTime;
        int duration;
    }

    public static String generateReport(ExamStats stats) {
        StringBuilder html = new StringBuilder();

        // 生成参与情况部分
        String school = stats.schoolName;
        if (!stats.campusName.equals(school)) {
            school = String.format("%s（%s）", school, stats.campusName);
        }

        html.append("<div class='report'>\n");
        html.append("<h1>").append(school).append("</h1>\n");

        html.append("<h2>参与情况</h2>\n");
        html.append("<ul>\n");
        html.append(String.format("<li>参与人数：%d人</li>\n", stats.totalStudents));
        html.append(String.format("<li>交卷人数：%d人（%.0f%%）</li>\n",
                stats.submittedStudents,
                (double)stats.submittedStudents/stats.totalStudents * 100));
        html.append(String.format("<li>参与班级：%d个</li>\n", stats.totalClasses));
        html.append("</ul>\n");

        html.append("<p>前10个班级：</p>\n");
        html.append("<ul>\n");
        for (ClassInfo cls : stats.topClasses) {
            html.append(String.format("<li>%s %d人</li>\n", cls.className, cls.studentCount));
        }
        html.append("</ul>\n");

        // 生成客观题得分统计部分
        html.append("<h2>客观题得分统计</h2>\n");
        html.append("<ul>\n");
        html.append(String.format("<li>最低分：%d 分</li>\n", stats.minScore));
        html.append(String.format("<li>最高分：%d 分</li>\n", stats.maxScore));
        html.append(String.format("<li>平均分：%.0f 分</li>\n", stats.avgScore));
        html.append(String.format("<li>高分率（>=%d）：%.0f%%</li>\n", stats.highScore, stats.highScoreRate * 100));
        html.append("</ul>\n");

        html.append("<p>各区间分布：</p>\n");
        html.append("<ul>\n");
        stats.scoreDistribution.forEach((range, percentage) ->
                html.append(String.format("<li>%s: %.2f%%</li>\n", range, percentage)));
        html.append("</ul>\n");

        // 生成前5名表格
        html.append(String.format("<p>前%d名（提交时间低于20分钟不参与排名）：</p>\n", stats.topStudents.size()));
        html.append("<table border='1'>\n");
        html.append("<tr><th>名次</th><th>班级</th><th>姓名</th><th>分数</th><th>提交时间</th><th>用时（分钟）</th></tr>\n");

        for (StudentRank student : stats.topStudents) {
            html.append(String.format("<tr><td>%d</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%d</td></tr>\n",
                    student.rank,
                    student.className,
                    student.name,
                    student.score,
                    Times.sDT(student.submitTime),
                    student.duration));
        }
        html.append("</table>\n");

        // 生成交卷时间统计部分
        html.append("<h2>交卷时间（分钟）统计</h2>\n");
        html.append("<ul>\n");
        html.append(String.format("<li>最小值: %d 分钟</li>\n", stats.minTime));
        html.append(String.format("<li>最大值: %d 分钟</li>\n", stats.maxTime));
        html.append(String.format("<li>平均值: %.2f 分钟</li>\n", stats.avgTime));
        html.append("</ul>\n");

        html.append("<p>各区间分布占比:</p>\n");
        html.append("<ul>\n");
        stats.timeDistribution.forEach((range, percentage) ->
                html.append(String.format("<li>%s: %.2f%%</li>\n", range, percentage)));
        html.append("</ul>\n");

        html.append("</div>");
        return html.toString();
    }
}